package com.smartphonedev.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.smartphonedev.function.Ringtones;
import com.smartphonedev.function.SmsFunc;
import com.smartphonedev.variables.Constants;


public class Tab2Activity extends Activity {

	final Context context = this;
	Button bt;
	EditText et;
	Spinner spinner;
	SharedPreferences sp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab2);
		bt = (Button) findViewById(R.id.TAB2BUTTON);
		et = (EditText) findViewById(R.id.TAB2EDITPHONE);
		spinner = (Spinner) findViewById(R.id.TAB2EDITSPINNER);
		sp = getSharedPreferences(Constants.foldername, 0);
		String set = sp.getString(Constants.tab2number, "");
		String sspinner = sp.getString(Constants.tab2spinner, "1");
		int converts = Integer.parseInt(sspinner);
		sp.getString(Constants.tab2spinner, "");
		et.setText(set);
		List<String> list = new ArrayList<String>();
		list.add("0");
		list.add("1");
		list.add("2");
		list.add("3");
		list.add("4");
		list.add("5");
		list.add("6");
		list.add("7");
		list.add("8");
		list.add("9");
		list.add("10");
		list.add("11");
		list.add("12");
		list.add("13");
		list.add("14");
		list.add("15");
		list.add("16");
		list.add("17");
		list.add("18");
		list.add("19");
		list.add("20");
		list.add("21");
		list.add("22");
		list.add("23");
		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, list);
		dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(dataAdapter);
		spinner.setSelection(converts);
	}


	public void setButtonActiveTracking(View v) {
		setButtonActiveTracking();
	}

	public void setButtonActiveTracking() {
		String number = et.getText().toString();
		if (number.equals("")) {
			Toast.makeText(context, context.getString(R.string.error12), Toast.LENGTH_LONG).show();
			Ringtones.play2(context, R.raw.notifynotsuccess);
		} else {
			SharedPreferences.Editor editor = sp.edit();
			editor.putString(Constants.tab2number, number);
			editor.putString(Constants.tab2spinner, String.valueOf(spinner.getSelectedItem()));
			editor.commit();
			SmsFunc.startSMSAlert(context);
			AlertDialog.Builder adb = new AlertDialog.Builder(this);
			adb.setTitle(R.string.confirm);
			adb.setIcon(R.drawable.info2);
			adb.setMessage(context.getString(R.string.tab2dialogmsg) + " " + number + " " + context.getString(R.string.time2) + " " + String.valueOf(spinner.getSelectedItem()) + " " + context.getString(R.string.hour));
			adb.setCancelable(false);
			adb.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
			adb.show();
			bt.setText(context.getString(R.string.closetracking));
			bt.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					setButtonCloseTracking();

				}
			});

		}
	}

	public void setButtonCloseTracking() {
		SharedPreferences.Editor editor = sp.edit();
		editor.putString(Constants.tab2number, "");
		editor.putString(Constants.tab2spinner, "1");
		editor.commit();
		et.setText("");
		spinner.setSelection(0);
		bt.setText(context.getString(R.string.activetracking));
		bt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setButtonActiveTracking();

			}
		});
		AlertDialog.Builder adb = new AlertDialog.Builder(this);
		adb.setTitle(R.string.confirm);
		adb.setIcon(R.drawable.info2);
		adb.setMessage(context.getString(R.string.closetrackingdone));
		adb.setCancelable(false);
		adb.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		adb.show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu2, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.menu_exit2:
			Tab2Activity.this.finish();
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		super.onKeyDown(keyCode, event);
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			AlertDialog.Builder adb = new AlertDialog.Builder(this);
			adb.setTitle(R.string.confirm);
			adb.setIcon(R.drawable.exit);
			adb.setMessage(R.string.confirmexit);
			adb.setCancelable(false);
			adb.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					Tab2Activity.this.finish();
				}
			});
			adb.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
				}
			});
			adb.show();

			return true;
		}
		return false;
	}

}
